package tree;

import file.Files;
import printer.BinaryTrees;

import java.util.Comparator;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/3/4 13:22
 */
public class Main {
    /**
     * 自定义比较器
     */
    private static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    /**
     * 自定义比较器2
     */
    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test1() {
        //定义数组
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1, 6, 10};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        //打印 二叉搜索树
        BinaryTrees.println(bst);
        //BinaryTrees.println(bst, BinaryTrees.PrintStyle.LEVEL_ORDER);
        Files.writeToFile("d:/a.txt", bst);
    }

    static void test0() {
        //定义数组
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        //创建对象
        BinarySearchTree<Person> bst = new BinarySearchTree();

        for (int i = 0; i < arr.length; i++) {
            bst.add(new Person(arr[i]));
        }
        BinaryTrees.print(bst);

    }

    static void test2() {
        //1 也可以内部类实现 匿名类
        BinarySearchTree<Person> b2 = new BinarySearchTree(new PersonComparator());
        b2.add(new Person(12));
        b2.add(new Person(15));
        b2.add(new Person(9));
        BinaryTrees.println(b2);
    }

    static void test3() {
        //2
        BinarySearchTree<Person> b3 = new BinarySearchTree(new PersonComparator2());
        b3.add(new Person(12));
        b3.add(new Person(15));
        b3.add(new Person(9));
        BinaryTrees.println(b3);

    }

    static void test4() {
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 1, 3, 10, 12};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        //打印 二叉搜索树
        BinaryTrees.println(bst);

        System.out.println("前序遍历：");
        bst.preorderTraversal();
        System.out.println();
        System.out.println("中序遍历：");
        bst.ineorderTraversal();
        System.out.println();
        System.out.println("后序遍历：");
        bst.postorderTraversal();
        System.out.println();
        System.out.println("层序遍历：");
        bst.levelOrder();
        System.out.println();

        /**
         * 设计模式 相关问题
         * 拿到 元素
         */
        bst.levelOrder(new BinarySearchTree.Visitor() {
            @Override
            public boolean visit(Object element) {
                System.out.print(element + " ");
                return false;
            }
        });
        System.out.println();
        System.out.println("*****************************");

        bst.preorder(new BinarySearchTree.Visitor() {
            @Override
            public boolean visit(Object element) {
                System.out.print(element + "_");
                return false;
            }
        });
    }

    static void test6() {
        Integer arr[] = {7, 4, 9, 2, 5};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        //打印 二叉搜索树
        BinaryTrees.println(bst);
        System.out.print("前序遍历：");
        bst.preorder(new BinarySearchTree.Visitor() {
            @Override
            public boolean visit(Object element) {
                System.out.print(element + " ");
                if ((Integer) element == 2) {
                    return true;
                }
                return false;
            }
        });
        System.out.println();
        System.out.print("层序遍历：");
        bst.levelOrder(new BinarySearchTree.Visitor() {
            @Override
            public boolean visit(Object element) {
                System.out.print(element + " ");
                if ((Integer) element == 9) {
                    return true;
                }
                return false;
            }
        });
    }

    static void test7() {
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 1, 3};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        BinaryTrees.println(bst);

        System.out.println(bst);
    }

    static void test8() {
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 1, 3, 0};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.height2());
        //System.out.println(bst);
    }

    /***
     * 随机生成 树
     */
    static void test9(){
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();

        for(int i=0;i<10;i++){
            bst.add((int)(Math.random()*100));
        }
        BinaryTrees.print(bst);
        System.out.println();
        System.out.println(bst.isComplate());
    }

    static void test10(){
        Integer arr[] = {7, 4, 9, 2, 5, 8, 11, 1, 3};
        //创建对象
        BinarySearchTree bst = new BinarySearchTree();
        //循环存入数据
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        BinaryTrees.print(bst);
        System.out.println();
        System.out.println(bst.isComplate());
    }

    public static void main(String[] args) {
        //test1();
        //test0();
        //test2();
        //System.out.println("*****************************");
        //test3();
        //test4();
        //test6();
        //test7();
        //test8();
        //test9();
        test10();

    }
}
